package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import br.urca.dao.SolicitacaoDAO;
import br.urca.model.Solicitacao;

public class JPASolicitacaoDAO implements SolicitacaoDAO {

	private final EntityManager manager;

	@Inject
	public JPASolicitacaoDAO(EntityManager manager) {
		this.manager = manager;
	}

	public JPASolicitacaoDAO() {
		this(null);
	}

	@Override
	public Solicitacao getSolicitacao(String numProtocolo, String periodo,
			String codRequerimento) {
		try {
			return this.manager
					.createQuery(
							"SELECT s FROM Solicitacao s "
									+ "WHERE s.id.periodo = :periodo "
									+ "AND s.id.numProtocolo = :protocolo "
									+ "AND s.id.codRequerimento = :requerimento",
							Solicitacao.class).setParameter("periodo", periodo)
					.setParameter("protocolo", numProtocolo)
					.setParameter("requerimento", codRequerimento)
					.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public boolean salvarSolicitacao(Solicitacao solicitacao) {
		return this.manager.merge(solicitacao) != null;
	}

}
